﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using IPMSG;

namespace IPMsgMoTest.ViewModels
{
    public class ChatViewModel : INotifyPropertyChanged
    {
        public ChatViewModel()
        {
            this.ChatItems = new ObservableCollection<ChatItemViewModel>();
        }

        public ObservableCollection<ChatItemViewModel> ChatItems { get; private set; }

        private bool isReading;

        public bool IsReading
        {
            get { return isReading; }
            set
            {
                isReading = value;
                if (this.isReading == true)
                {
                    this.UnreadMsg = 0;
                }
            }
        }


        private string hostname;

        public string Hostname
        {
            get
            {
                return this.hostname;
            }
            set
            {
                if (value != this.hostname)
                {
                    this.hostname = value;
                    NotifyPropertyChanged("Hostname");
                }
            }
        }

        private int unreadMsg;

        public int UnreadMsg
        {
            get
            {
                return this.unreadMsg;
            }
            set
            {
                if (value > 0)
                {
                    this.BackColor = new SolidColorBrush(Color.FromArgb(0xFF, 0xAB, 0xBA, 0x15));
                }
                else
                {
                    this.BackColor = new SolidColorBrush(Color.FromArgb(0xFF, 0x20, 0x90, 0x12));
                }

                if (value != this.unreadMsg)
                {
                    this.unreadMsg = value;
                    NotifyPropertyChanged("UnreadMsg");
                }
            }
        }

        private SolidColorBrush backColor;

        public SolidColorBrush BackColor
        {
            get { return backColor; }
            set
            {
                if (this.backColor != value)
                {
                    backColor = value;
                    NotifyPropertyChanged("BackColor");
                }
            }
        }

        internal void HandleRcvMsg(string message)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                if (!this.isReading)
                {
                    this.UnreadMsg++;
                }

                this.ChatItems.Add(new ChatItemViewModel() { TimeStamp = DateTime.Now, ChatContent = message, ChatContentType = ChatType.Receive });
            });
        }

        internal void HandleSendMsg(string message)
        {
            this.ChatItems.Add(new ChatItemViewModel() { TimeStamp = DateTime.Now, ChatContent = message, ChatContentType = ChatType.Send });
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
